package com.material.web.controller.pound;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.material.common.core.domain.entity.SysUser;
import com.material.common.utils.DateUtils;
import com.material.common.utils.SecurityUtils;
import com.material.common.utils.poi.ExcelImportUtil;
import com.material.factory.domain.FactoryFuelAttribute;
import com.material.pound.params.FuelPoundImportParams;
import com.material.pound.params.FuelPoundUpdate;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.material.common.annotation.Log;
import com.material.common.core.controller.BaseController;
import com.material.common.core.domain.AjaxResult;
import com.material.common.enums.BusinessType;
import com.material.pound.domain.FuelPound;
import com.material.pound.service.IFuelPoundService;
import com.material.common.utils.poi.ExcelUtil;
import com.material.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 磅单信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/pound/pound")
public class FuelPoundController extends BaseController
{
    @Autowired
    private IFuelPoundService fuelPoundService;

    /**
     * 查询磅单信息列表
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuelPound fuelPound)
    {
        Long bizId = getBizId();
        boolean admin = SecurityUtils.isAdmin(getUserId());
        if(!admin){
            fuelPound.setFactoryId(bizId);
        }
        startPage();
        List<FuelPound> list = fuelPoundService.selectFuelPoundList(fuelPound);
        return getDataTable(list);
    }

    /**
     * 导出磅单信息列表
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:export')")
    @Log(title = "磅单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FuelPound fuelPound)
    {
        List<FuelPound> list = fuelPoundService.selectFuelPoundList(fuelPound);
        ExcelUtil<FuelPound> util = new ExcelUtil<FuelPound>(FuelPound.class);
        util.exportExcel(response, list, "磅单信息数据");
    }

    /**
     * 获取磅单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fuelPoundService.selectFuelPoundById(id));
    }

    /**
     * 新增磅单信息
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:add')")
    @Log(title = "磅单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FuelPound fuelPound)
    {
        return toAjax(fuelPoundService.insertFuelPound(fuelPound));
    }

    /**
     * 修改磅单信息
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:edit')")
    @Log(title = "磅单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FuelPoundUpdate fuelPound)
    {
        return toAjax(fuelPoundService.updateFuelPoundData(fuelPound));
    }

    /**
     * 删除磅单信息
     */
    @PreAuthorize("@ss.hasPermi('pound:pound:remove')")
    @Log(title = "磅单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fuelPoundService.deleteFuelPoundByIds(ids));
    }

    @Log(title = "化验结果导入", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        List<FuelPoundImportParams> fuelPoundImportParams = ExcelImportUtil.importExcel(file.getInputStream());
        String message = fuelPoundService.importAssay(fuelPoundImportParams);
        return success(message);
    }




}
